﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.BizTalk.ExplorerOM;
using DBLOG.BizTalk.BizTalkExplorerServices.Core;

namespace DBLOG.BizTalk.BizTalkExplorerServices.Entities
{
    public class BTSOrchestration : BTSEntityBase
    {
        private BtsOrchestration _orchestration { get; set; }

        List<BTSSendPort> BTSSendPorts { get; set; }
        List<BTSReceivePort> BTSReceivePorts { get; set; }

        public BTSOrchestration()
        {
        }

        internal BTSOrchestration(BtsOrchestration orchestration)
        {
            this._orchestration = orchestration;

            this.PopulateSimpleProperties();

            this.PopulateSendPorts();

            this.PopulateReceivePorts();           
        }

        private void PopulateSimpleProperties()
        {
            this.Name = this._orchestration.FullName;

            this.Details = this._orchestration.Description;

            this.ApplicationName = this._orchestration.Application.Name;

            this.Date = DateTime.Now;

            if (this._orchestration.Status == OrchestrationStatus.Started)
            {
                this.Status = Core.Status.Started;
            }
            else
            {
                this.Status = Core.Status.Stopped;
            }
        }

        private void PopulateSendPorts()
        {
            var sendPorts = from s in this._orchestration.Ports.Cast<OrchestrationPort>().ToList<OrchestrationPort>()
                            where s.SendPort != null
                            select new BTSSendPort(s.SendPort);

            this.BTSSendPorts = sendPorts.ToList<BTSSendPort>();
        }

        private void PopulateReceivePorts()
        {
            var receivePorts = from r in this._orchestration.Ports.Cast<OrchestrationPort>().ToList<OrchestrationPort>()
                            where r.ReceivePort != null
                            select new BTSReceivePort(r.ReceivePort);

            this.BTSReceivePorts = receivePorts.ToList<BTSReceivePort>();
        } 
    }
}